/**
 * Created by ascainiao on 2014/11/22.
 * 邮箱405076856@qq.com
 * CreateJS中文网 https://www.createjs.cc
 * QQ交流群：320648191
 * 双指缩放助手，适用于creaetjs。
 */
import createjs from 'createjs-ts'
import cjs=createjs
import { GameConfig } from '../Const';
export default class DoubleFingerScaleHelper{
    private target:cjs.DisplayObject;
    /*private ratio=1;
    private scale=1
    private distance=0
    private maxScale=Number.MAX_VALUE
    private minScale=Number.MIN_VALUE*/
    private mouseDownHandler:any
    private pressMoveHandler:any
    /**
     * 双指缩放助手，适用于creaetjs。
     * @param target 缩放的目标对象
     * @param ratio 设备像素比除以容器缩放比
     * @param max 缩放比最大值
     * @param min 缩放比最小值
     * @param mouseDownCallback 点下回调 参数：p0触点坐标,p1触点坐标,p2双指中间点坐标
     * @param pressMoveCallback 滑动回调 参数：p0触点坐标,p1触点坐标,p2双指中间点坐标
     */
    constructor(target:cjs.DisplayObject,ratio=1,max=Number.MAX_VALUE,min=Number.MIN_VALUE,mouseDownCallback?:(p0:cjs.Point,p1:cjs.Point,p2:cjs.Point)=>void,pressMoveCallback?:(p0:cjs.Point,p1:cjs.Point,p2:cjs.Point)=>void){
        this.target=target
        /*this.ratio=ratio
        this.maxScale=max
        this.minScale=min
        this.mouseDownCallback=mouseDownCallback
        this.pressMoveCallback=pressMoveCallback*/
        let d:number,scale=1
        //
        this.mouseDownHandler=target.stage.on('stagemousedown',(e:cjs.MouseEvent)=>{
            const evt = e.nativeEvent as TouchEvent
            if(evt.touches?.length>1){
                //const touch0=evt.touches[0],touch1=evt.touches[1]
                const p0=this.getStagePoint(evt.touches[0],ratio)
                const p1=this.getStagePoint(evt.touches[1],ratio)
                const p2=new cjs.Point((p0.x+p1.x)/2,(p0.y+p1.y)/2)
                const reg=target.globalToLocal(p2.x,p2.y)
                const localp=target.parent.globalToLocal(p2.x,p2.y)
                scale=target.scale
                this.target.setTransform(localp.x,localp.y,scale,scale,0,0,0,reg.x,reg.y)
                d=Math.sqrt(Math.pow(p0.x-p1.x,2)+Math.pow(p0.y-p1.y,2))
                mouseDownCallback&&mouseDownCallback(p0,p1,p2)
            }
        })
        this.pressMoveHandler=this.pressMoveHandler=target.stage.on('stagemousemove',(e:cjs.MouseEvent)=>{
            const evt = e.nativeEvent as TouchEvent
            if(evt.touches?.length>1){
                const p0=this.getStagePoint(evt.touches[0],ratio)
                const p1=this.getStagePoint(evt.touches[1],ratio)
                const p2=new cjs.Point((p0.x+p1.x)/2,(p0.y+p1.y)/2)
                //const reg=this.target.globalToLocal(p2.x,p2.y)
                const localp=target.parent.globalToLocal(p2.x,p2.y)
                const t=Math.sqrt(Math.pow(p0.x-p1.x,2)+Math.pow(p0.y-p1.y,2))
                const f=t-d
                target.scale=scale+f/500;
                if(target.scale<min)target.scale=min
                if(target.scale>max)target.scale=max
                target.x=localp.x;
                target.y=localp.y;
                pressMoveCallback&&pressMoveCallback(p0,p1,p2)
            }
        })
        /*const canvas=target.stage.canvas as HTMLCanvasElement
        canvas.addEventListener('touchstart',this.onTouchStart.bind(this))
        canvas.addEventListener('touchmove',this.onTouchMove.bind(this))*/
    }
    /*private onTouchStart(evt:TouchEvent):void{
        if(evt.touches.length>1){
            const p0=new cjs.Point(evt.touches[0].clientX*this.ratio,evt.touches[0].clientY*this.ratio)
            const p1=new cjs.Point(evt.touches[1].clientX*this.ratio,evt.touches[1].clientY*this.ratio)
            const p2=new cjs.Point((p0.x+p1.x)/2,(p0.y+p1.y)/2)
            const reg=this.target.globalToLocal(p2.x,p2.y)
            const localp=this.target.parent.globalToLocal(p2.x,p2.y)
            this.scale=this.target.scale
            this.target.setTransform(localp.x,localp.y,this.scale,this.scale,0,0,0,reg.x,reg.y)
            this.distance=Math.sqrt(Math.pow(p0.x-p1.x,2)+Math.pow(p0.y-p1.y,2))
            this.mouseDownCallback&&this.mouseDownCallback(p0,p1,p2)
        }
    }*/
    /*private onTouchMove(evt:TouchEvent):void{
        if(evt.touches.length>1){
            const p0=new cjs.Point(evt.touches[0].clientX*this.ratio,evt.touches[0].clientY*this.ratio)
            const p1=new cjs.Point(evt.touches[1].clientX*this.ratio,evt.touches[1].clientY*this.ratio)
            const p2=new cjs.Point((p0.x+p1.x)/2,(p0.y+p1.y)/2)
            //const reg=this.target.globalToLocal(p2.x,p2.y)
            const localp=this.target.parent.globalToLocal(p2.x,p2.y)
            const t=Math.sqrt(Math.pow(p0.x-p1.x,2)+Math.pow(p0.y-p1.y,2))
            const f=t-this.distance
            this.target.scale=this.scale+f/500;
            if(this.target.scale<this.minScale)this.target.scale=this.minScale
            if(this.target.scale>this.maxScale)this.target.scale=this.maxScale
            this.target.x=localp.x;
            this.target.y=localp.y;
            this.pressMoveCallback&&this.pressMoveCallback(p0,p1,p2)
        }
    }*/
    private getStagePoint(touch:Touch,ratio=1):cjs.Point{
        let pageX=touch.pageX,pageY=touch.pageY
        if(GameConfig.screen_rotate==90){
            pageX=pageY
            pageY=window.innerWidth-touch.pageX
        }
        return new cjs.Point(pageX*ratio,pageY*ratio)
    }
    //销毁
    destroy():void{
        this.target.stage.off('stagemousedown',this.mouseDownHandler)
        this.target.stage.off('stagemousemove',this.pressMoveHandler)
        this.target=null
    }
}